package com.demo.controller;

import com.demo.dao.Course;
import com.demo.dao.Score;
import com.demo.service.CourseService;
import com.demo.service.GpaService;
import com.demo.service.ScoreService;
import com.demo.service.StudentService;
import com.demo.util.CalcuUtils;
import com.demo.util.Header;
import com.demo.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Api
public class ScoreController {

    @Autowired
    ScoreService scoreService;
    @Autowired
    CourseService courseService;
    @Autowired
    StudentService studentService;
    @Autowired
    GpaService gpaService;

    @ApiOperation("查询班级单一课程成绩")
    @RequestMapping("/getScoreByCname")
    public Result getScoreByCname(String classno, String term,String cname){

        int cid = courseService.getCidByName(cname);

        List<Score> data = scoreService.getScoreByCId(classno,term,cid);

        Result result = new Result();
        List<Header> headers = new ArrayList<>();
        headers.add(new Header("sid","学号"));
        headers.add(new Header("name","姓名"));
        headers.add(new Header("mark","成绩"));
        headers.add(new Header("credit","绩点"));

        data.sort(Comparator.comparingInt(Score::getMark));
        List<HashMap<String,String>> redata = new ArrayList<>();

        for (Score i: data
             ) {
            HashMap<String,String> temp = new HashMap<>();
            temp.put("sid",i.getSid());
            temp.put("name",studentService.getStuByNo(i.getSid()).getName());
            temp.put("mark", String.valueOf(i.getMark()));
            temp.put("credit", String.format("%.2f",CalcuUtils.calcuCredit(i.getMark(),i.getCredit())));
            redata.add(temp);
        }
        result.setHeaders(headers);
        result.setData(redata);
        return result;
    }

    @ApiOperation("查询班级单科不及格名单")
    @RequestMapping("/getDisqualifiedByCid")
    public Result getDisqualifiedByCid(String classno,String term,String cname){

        int cid = courseService.getCidByName(cname);

        List<Score> data = scoreService.getDisqualifiedByCid(classno,term,cid);

        Result result = new Result();
        List<Header> headers = new ArrayList<>();
        headers.add(new Header("sid","学号"));
        headers.add(new Header("name","姓名"));
        headers.add(new Header("mark","成绩"));
        headers.add(new Header("credit","绩点"));

        data.sort((o1, o2) -> o1.getMark() - o2.getMark());
        List<HashMap<String,String>> redata = new ArrayList<>();

        for (Score i: data
        ) {
            HashMap<String,String> temp = new HashMap<>();
            temp.put("sid",i.getSid());
            temp.put("name",studentService.getStuByNo(i.getSid()).getName());
            temp.put("mark", String.valueOf(i.getMark()));
            temp.put("credit", String.format("%.2f",CalcuUtils.calcuCredit(i.getMark(),i.getCredit())));

            redata.add(temp);
        }
        result.setHeaders(headers);
        result.setData(redata);
        return result;
    }

    @ApiOperation("根据学号查询成绩")
    @RequestMapping("/getScoreBySid")
    public Result getScoreBySid(String sid,String term){

        List<Course> courses = courseService.findCourseByTerm(term);
        List<Score> data = scoreService.getScoreBySid(term, sid);
        Result result = new Result();
        List<Header> headers = new ArrayList<>();
        headers.add(new Header("sid","学号"));
        headers.add(new Header("name","姓名"));
        headers.add(new Header("credit","学分"));
        for (Course i: courses
        ) {
            headers.add(new Header(String.valueOf(i.getCid()),i.getCname()));
        }

        data.sort((o1, o2) -> o1.getMark() - o2.getMark());
        List<HashMap<String,String>> redata = new ArrayList<>();
        HashMap<String,String> temp = new HashMap<>();
        temp.put("sid",sid);
        temp.put("name",studentService.getStuByNo(sid).getName());
        temp.put("credit", String.valueOf(gpaService.getCreditBySid(sid,term)));
        for (Score i: data) {
            temp.put(String.valueOf(i.getCid()),String.valueOf(i.getMark()));
        }
        redata.add(temp);
        result.setHeaders(headers);
        result.setData(redata);
        result.setCode(1);
        result.setMessage("success");
        return result;
    }

    @ApiOperation("总览班级学期成绩")
    @RequestMapping("/getAllScore")
    public Result getAllScore(String classno, String term){
        //本学期所有课程
        List<Course> courses = courseService.findCourseByTerm(term);
        int courseNum = courses.size();
        //生成header
        List<Header> header = new ArrayList<>();
        header.add(new Header("sid","学号"));
        header.add(new Header("name","姓名"));
        header.add(new Header("credit","学分"));
        header.add(new Header("rank","排名"));
        for (Course i: courses
             ) {
            header.add(new Header(String.valueOf(i.getCid()),i.getCname()));
        }
        //成绩表中所有记录
        List<Score> scoredata = scoreService.getScoreByClass(classno,term);
        int j=0;
        List<HashMap<String,String>> myUtils = new ArrayList<>();
        scoredata.sort((data1, data2) -> data1.getSid().compareTo(data2.getSid()));
        HashMap<String,String> temp = null;
        for (Score i: scoredata
             ) {
            if(j==courseNum){
                myUtils.add(temp);
                j=0;
            }
            if(j==0){
                temp = new HashMap<>();
                temp.put("sid",i.getSid());
                temp.put("name",studentService.getStuByNo(i.getSid()).getName());
                temp.put("credit",String.valueOf(i.getCredit()));
                temp.put("rank",String.valueOf(gpaService.getRankBySid(i.getSid(),term)));
            }

            temp.put(String.valueOf(i.getCid()),String.valueOf(i.getMark()));
            j++;
        }
        Result result = new Result();
        result.setMessage("success");
        result.setCode(1);
        result.setData(myUtils);
        result.setHeaders(header);
        return result;
    }

}
